Skip to content

Conversation

@MattiasBuelens
Copy link
Contributor

@MattiasBuelens MattiasBuelens commented Oct 8, 2025

Previously, many BufferSource algorithms were using internal slots such as [[ByteOffset]] and [[ByteLength]] directly. However, with the addition of SharedArrayBuffer (#353, #1311) and [AllowResizable] (#982), this comes with extra caveats:

Transferring an ArrayBuffer must also take into account whether it should remain resizable or not. Fortunately, we can use the new ArrayBufferCopyAndDetach operation for that.

  • I added an optional preserveResizability parameter to "transfer an ArrayBuffer", so specifications can choose whether to preserve resizability or not. By default, this is not preserved. (Most specs aren't using [AllowResizable] yet anyway.)
  • I removed the realm parameter of that algorithm, since ArrayBufferCopyAndDetach doesn't accept such a parameter. According to WebDex, no specs were actually using that parameter anyway.

Open questions:

Fixes #1312.
Fixes #1385.

  • At least two implementers are interested (and none opposed):
  • Tests are written and can be reviewed and commented upon at:
  • Implementation bugs are filed:
    • Chromium: …
    • Gecko: …
    • WebKit: …
    • Deno: …
    • Node.js: …
    • webidl2.js: …
    • widlparser: …
  • MDN issue is filed: …
  • The top of this comment includes a clear commit message to use.

(See WHATWG Working Mode: Changes for more details.)


Preview | Diff

@MattiasBuelens MattiasBuelens marked this pull request as ready for review October 13, 2025 08:14
@MattiasBuelens
Copy link
Contributor Author

@annevk Could you assign reviewer(s) to this? Thanks! 🙏

@annevk
Copy link
Member

annevk commented Nov 24, 2025

Thanks for tackling this!

Hopefully @syg and @bakkot can help or appoint someone?

Copy link
Member

@annevk annevk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did an editorial pass.

MattiasBuelens and others added 2 commits November 24, 2025 10:03
Co-authored-by: Anne van Kesteren <[email protected]>
Copy link
Contributor Author

@MattiasBuelens MattiasBuelens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quick self-review.

1. [=/Assert=]: |jsBufferSource| is a [=typed array type=] instance.
1. Let |taRecord| be [$MakeTypedArrayWithBufferWitnessRecord$](|jsBufferSource|,
Seq-Cst).
1. Return [$TypedArrayByteLength$](|taRecord|).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypedArrayByteLength asserts that IsTypedArrayOutOfBounds(taRecord) is false, which we didn't explicitly check. We should check for that, to align with %TypedArray%.prototype.byteLength.

Suggested change
1. Return [$TypedArrayByteLength$](|taRecord|).
1. If [$IsTypedArrayOutOfBounds$](|taRecord|) is true, return 0.
1. Return [$TypedArrayByteLength$](|taRecord|).

Comment on lines +9343 to +9348
1. Otherwise, if [$IsSharedArrayBuffer$](|jsBufferSource|) is true:
1. [=/Assert=]: |jsBufferSource| is a {{SharedArrayBuffer}}.
1. Return [$ArrayBufferByteLength$](|jsBufferSource|, Seq-Cst).
1. Otherwise:
1. [=/Assert=]: |jsBufferSource| is an {{ArrayBuffer}}.
1. Return |jsBufferSource|.\[[ArrayBufferByteLength]].
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArrayBufferByteLength supports both ArrayBuffer and SharedArrayBuffer, so we can combine these steps.

Suggested change
1. Otherwise, if [$IsSharedArrayBuffer$](|jsBufferSource|) is true:
1. [=/Assert=]: |jsBufferSource| is a {{SharedArrayBuffer}}.
1. Return [$ArrayBufferByteLength$](|jsBufferSource|, Seq-Cst).
1. Otherwise:
1. [=/Assert=]: |jsBufferSource| is an {{ArrayBuffer}}.
1. Return |jsBufferSource|.\[[ArrayBufferByteLength]].
1. Otherwise:
1. [=/Assert=]: |jsBufferSource| is an {{ArrayBuffer}} or a {{SharedArrayBuffer}}.
1. Return [$ArrayBufferByteLength$](|jsBufferSource|, Seq-Cst).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

"byte length of a buffer source type" needs updating for resizable and detached buffers Buffer source types infrastructure

2 participants